﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BaoVeThanhDia.Buttons
{
    class Button
    {
        string name;
        string link;
        Texture2D Image;
        Vector2 Position;
        float scale;
        Vector2 Offset, SizeImage;
        bool isTouched;
        int countDraw;
        public Button(string name, string link, Vector2 posi, Vector2 offset, Vector2 size, float scale)
        {
            countDraw = 0;
            this.name = name;
            this.link = link;
            this.scale = scale;
            Position = posi;
            SizeImage = size;
            isTouched = false;
            this.Offset = offset;
        }
        public void Load(ContentManager cont)
        {
            Image = cont.Load<Texture2D>(link);
        }
        public void Draw(SpriteBatch sprite, Vector2 Dolech, float mscale = 0)
        {
            if (mscale == 0)
            {
                sprite.Draw(Image, (isTouched ? (Position - SizeImage * 0.1f) : Position) + Dolech, new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White, 0f, Vector2.Zero, isTouched ? scale + 0.2f : scale, SpriteEffects.None, 0f);
            }
            else
            {
                sprite.Draw(Image, (isTouched ? (Position - SizeImage * 0.1f) : Position) * mscale + Dolech, new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White, 0f, Vector2.Zero, isTouched ? mscale + 0.2f : mscale, SpriteEffects.None, 0f);
            }
        }
        public void ZoomDraw(SpriteBatch sprite, float beginScale, float EndScale)
        {
            countDraw++;
            if (countDraw > 20) countDraw = 20;
            scale = beginScale + (EndScale - beginScale) * countDraw / 20;
            sprite.Draw(Image, Position - SizeImage * (scale - beginScale) / 2, new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
        }
        public void OptionDraw(SpriteBatch sprite, Vector2 posi)
        {
            sprite.Draw(Image, (isTouched ? (posi - SizeImage * 0.1f) : posi), new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White, 0f, Vector2.Zero, isTouched ? scale + 0.2f : scale, SpriteEffects.None, 0f);
        }
        public void TimeAgainDraw(SpriteBatch sprite, int CurrentPercent)
        {
            if (CurrentPercent >= 100)
                sprite.Draw(Image, (isTouched ? (Position - SizeImage * 0.1f) : Position), new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White, 0f, Vector2.Zero, isTouched ? scale + 0.2f : scale, SpriteEffects.None, 0f);
            else
            {
                sprite.Draw(Image, Position, new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)SizeImage.Y), Color.White * 0.4f, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
                sprite.Draw(Image, Position, new Rectangle((int)Offset.X, (int)Offset.Y, (int)SizeImage.X, (int)(CurrentPercent * SizeImage.Y / 100)), Color.White * 0.4f, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            }
        }
        public bool isTouch(Vector2 touch)
        {
            isTouched = false;
            if (touch.X >= Position.X && touch.Y >= Position.Y && touch.X <= (Position.X + SizeImage.X * scale) && touch.Y <= (Position.Y + SizeImage.Y * scale))
            {
                isTouched = true;
                return true;
            }
            return false;
        }
        public bool isTouch(Vector2 touch, Vector2 Dolech, float mscale = 1)
        {
            isTouched = false;
            touch -= Dolech;
            if (touch.X >= Position.X && touch.Y >= Position.Y && touch.X <= (Position.X + SizeImage.X) && touch.Y <= (Position.Y + SizeImage.Y))
            {
                isTouched = true;
                return true;
            }
            return false;
        }
        public string getName() { return name; }
        public void setTouch(bool k) { isTouched = k; }
        public float getScale() { return scale; }
    }
}
